package com.atguigu.yygh.cmn.controller;

import com.atguigu.yygh.cmn.mapper.DictMapper;
import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.common.R;
import com.atguigu.yygh.model.cmn.Dict;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author zhmwi
 * @Date 2021/7/5
 * @Dersciption
 */
@RestController
@Api("数据字典接口")
@RequestMapping("/admin/cmn/dict/")
public class DictController {
    @Autowired
    private DictService dictService;

    @ApiOperation("根据数据id查询子数据列表")
    @GetMapping("queryTreeChildById/{id}")
    public R queryTreeChildById(@PathVariable Long id){
        List<Dict> dicts = dictService.queryTreeChildById(id);
        return R.ok().data("dicts",dicts);
    }

    @ApiOperation(value="导出")
    @GetMapping(value = "/exportData")
    public void exportData(HttpServletResponse response) {
        dictService.exportData(response);
    }

    @ApiOperation(value = "导入")
    @PostMapping("importData")
    public R importData(MultipartFile file) {
        dictService.importData(file);
        return R.ok();
    }

    //供远程调用使用
    @ApiOperation("根据字典类型编号和字典唯一value值获取字典对应名称")
    @GetMapping("getDictionaryName/{dictCode}/{value}")
    public String getDictionaryNameByDictCodeAndValue(@PathVariable("dictCode") String dictCode,
                                                      @PathVariable("value") String value){
        String dictName = dictService.getDictionaryNameByDictCodeAndValue(dictCode,value);
        return dictName;
    }
    @ApiOperation("根据字典唯一value值获取字典对应普通名称")
    @GetMapping("getDictionaryName/{value}")
    public String getDictionaryNameByDictCodeAndValue(@PathVariable("value") String value){
        String dictName = dictService.getDictionaryNameByDictCodeAndValue("",value);
        return dictName;
    }

    //根据dictCode获取下级结点
    @ApiOperation("根据dictCode获取下级节点")
    @GetMapping("/getByDictCode/{dictCode}")
    public R getByDictCode(@PathVariable("dictCode") String dictCode){
        List<Dict> childList = dictService.findByDictCode(dictCode);
        return R.ok().data("childList",childList);
    }
    //根据dictId获取下级节点集合
    @ApiOperation("根据dictId获取下级节点集合")
    @GetMapping("/getByDictId/{dictId}")
    public R getByDictId(@PathVariable("dictId") long dictId){
        List<Dict> childList = dictService.getByDictId(dictId);
        return R.ok().data("childList",childList);
    }

}
